package com.example.javabasic.suanfa.jiandan.二分查找;

/**
 * @description 二分查找
 * @Author Lin FuYuan
 * @date 2021/7/2
 */
public class Demo {
    public static void main(String[] args) {
        int[] nums = {1,3,4,5,6,7,9,10};
        Boolean search = search(nums, 0, nums.length - 1, 8);
        System.out.println(search);
    }

    /**
     * 二分查找算法
     * @author Lin FuYuan
     * @param nums 有序数组
     * @param min 最小下标
     * @param max 最大下标
     * @param target 目标值
     * @date 2021/7/2
     * @return java.lang.Boolean
     */
    public static Boolean search(int[] nums ,int min,int max ,int target){
         while(min<=max){
             //获取 中间的数字下标(两下标相加除2)
             int now = (max + min)/2;
             System.out.println(now+"->"+ nums[now]);
             if(target == nums[now]){
                 return true;
             }else if(nums[now]> target){
                max = now -1;
             }else if(nums[now]<target){
                 min = now +1;
             }
         }
         return false;
    }
}
